Python Scripting for Network Engineers: Realizing Network Automation for Reliable Networks by Hassan Wajid
Author:Hassan, Wajid [Hassan, Wajid]
Language: eng
Format: epub
Publisher: UNKNOWN
Published: 2019-05-27T16:00:00+00:00
Chapter 6 Functions in Python Scripting
6.1 Objectives
The main objectives of this chapter are to discuss:
• Functions, Constructs and its Uses
• Built-in and User Defined Functions
• Discusses Object Oriented Programming
• Discusses Classes and its usage
6.2 Introduction
Python is a general-purpose high-level programming language. Like any other programming language, Python has some codes that are used more than once and hence need not to be rewritten over and over. This chapter discusses how the Python script can be modularized and made repeatable. These repeatable codes are called Functions.
Functions, Object Oriented Programming and Classes are discussed in this chapter that are used extensively in Python Scripts for writing large scripts which can handle many input requests, interrupts and generate kind of actions with changing conditions of the inputs and requirements.
The main objectives of this chapter include; being able to understand why and how programmers divide programs up into sets of cooperating functions, to be able to define new functions in Python and lastly to understand the details of function calls and parameter passing in Python.
6.2.1 Functions
Python has some pieces for code that have been organized into a block and can be reused more than once for a single related function. These blocks of codes are the ones referred to as functions of Python. They provide high-levels of reusing program codes and hence enhancing modularity in the program development process. Creating a customized function is easy in Python.
Code for creating a function
Figure 6-1 Code demonstration of creating a function
Figure 6-2 output in python
Functions in Python begin with the key-word def which is then followed by the name of the function and parenthesis. The parenthesis takes all the input parameters and the code block in every function begins with a colon. The return statement is used to exit any function and can be called in any other part of the program.
6.2.2 Object-oriented programming
Python is known to be a language. This is because almost everything is in Python is written in terms of objects which are associated with properties and methods.
6.2.3 Classes
Classes in Python are like object constructors and is created with the keyword class. Code for creating a class in python
Figure 6-3 Code demonstration of creating a function
Figure 6-4 Code output in python
Figure 6-5 Code representing how to create a class in Python
The use of functions and objects in Python programming makes it easy to design a functional code with full functionalities and be easily understandable (Ozer, 2017). Functions increase modularity in program codes and hence make them easy to be reused. This chapter mainly focuses on the programming using Python functions and classes that makes Python termed as an object-oriented programming language.
6.3 Program Components in Python
Python language has several components. These components include; numbers, strings, sets, lists, dictionaries, tuples and functions (Rivers & Koedinger, 2017). All these components work together in meeting the specifications of a given program code. Python combines its components in one program hence ensuring that a fully functional program which meets the needs of the users is designed in the process.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26532)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19015)
The Fifty Shades Trilogy & Grey by E L James(18969)
Shot Through the Heart by Mercy Celeste(18886)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16993)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16886)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16810)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16710)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16337)
The Subtle Art of Not Giving a F*ck by Mark Manson(14274)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14078)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13442)
Scorched Earth by Nick Kyme(12719)
Drei Generationen auf dem Jakobsweg by Stein Pia(10927)
Suna by Ziefle Pia(10851)
Scythe by Neal Shusterman(10280)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9483)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9320)
This is Going to Hurt by Adam Kay(9110)
